// Paypages/pages/good_detail/good_detail.js

// 引用api文件
const api = require('../../../utils/api.js');
// 引入封装的请求类
const {myRequest} = require('../../../utils/myRequest.js');
//在使用的View中引入WxParse模块
var WxParse = require('../../../libs/wxParse/wxParse');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    issue: [],
    attribute: [],
    brand: '',
    productList:[],     //需要加入购物车的商品
    comment: [],
    gallery: '',
    goodsList: [],    // 商品详情页的关联商品（大家都在看）
    current: 1,
    showNumSelect: false,
    showServer: false,
    mainScroll:'srcoll',
    mainHeight: '100%',
    zNumber: '99',
    selectNum: 1,
    bdColor: '#f4f4f4',
    isOnBuy: false,
    isOnCart: false,
    collect: false
  },

  slideTo(e){
    console.log('slideTo',e);
    let current = e.detail.current;
    current +=1;
    this.setData({
      current: current
    })
  },

  preventTouchMove(){},

  // 控制选择商品的数量加减
  toUp(){
    let selectNum = this.data.selectNum;
    // 判断如果selectNum为1 不能再减 跳出方法
    if(selectNum > 1){
      selectNum -= 1;
      this.setData({
        selectNum: selectNum
      })
    }
    if(selectNum == 1){
      this.setData({
        bdColor: '#f4f4f4'
      })
      return;
    }
  },
  toAdd(){
    let selectNum = this.data.selectNum;
    selectNum += 1;
    this.setData({
      selectNum: selectNum,
      bdColor: 'rgb(228, 226, 226)'
    })
  },

  // 控制数量选择弹窗显示和隐藏
  toNumSelect(){
    this.setData({
      showNumSelect: true,
      zNumber: '9999'
    })
  },
  hidNumSelect(){
    this.setData({
      showNumSelect: false,
      zNumber: '99',
      isOnBuy: false,
      isOnCart: false,
      selectNum: 1
    })
  },

  // 点击购买按钮 
  onBuy(){
    this.setData({
      showNumSelect: true,
      zNumber: '99',
      isOnBuy: true,
      isOnCart: false
    })
  },
  // 点击加入购物车按钮
  onCart(){
    this.setData({
      showNumSelect: true,
      zNumber: '99',
      isOnBuy: false,
      isOnCart: true
    })
  },

  // 点击确认按钮
  onConfirm(){
    let isOnBuy = this.data.isOnBuy;
    let isOnCart = this.data.isOnCart;
    if(isOnBuy){
      wx.showToast({
        title: '购买成功！',
        icon: 'success',
        duration: 1500
      });
      this.setData({
        isOnBuy: false,
        showNumSelect: false,
        selectNum: 1
      })
    }
    if(isOnCart){
      wx.showToast({
        title: '添加成功，在购物车等亲~',
        icon: 'none',
        duration: 1500
      });
      this.setData({
        isOnCart: false,
        showNumSelect: false,
        selectNum: 1
      })
  }
  },

  // 点击 联系客服
  onCall(){
    wx.showToast({
      title: '我的手机号码是10086，请不要白天联系，打扰我休息~',
      icon: 'none',
      duration: 3000
    })
  },

  // 点击添加或取消收藏
  onCollect(){
    let id = this.data.info.id;
    myRequest({
      url: api.addordelCollect,
      method: 'post',
      data: { typeId: 0, valueId: id },
      success: (res)=>{
        console.log('addordelCollect',res);
      }
    });
    let collect = this.data.collect;
    if(collect == false){
      wx.showToast({
        title: '收藏成功!',
        icon: 'success',
        duration: 1000
      });
      this.setData({
        collect: true
      });
    }
    if(collect == true){
      wx.showToast({
        title: '取消收藏成功!',
        icon: 'none',
        duration: 1000
      });
      this.setData({
        collect: false
      });
    }
  },

  // 点击 跳转到填写地址的页面
  toAddrPage(){
    wx.navigateTo({
      url: '../../../minePackge/pages/useraddr/useraddr',
    })
  },

  // 控制服务弹窗显示和隐藏
  toServe(){
    this.setData({
      showServer: true,
      mainScroll:'hidden',
      mainHeight: '100vh'
    })
  },
  hidServe(){
    this.setData({
      showServer: false,
      mainScroll:'scroll',
      mainHeight: '100%'
    })
  },
  // 添加商品到购物车
  addcart(){
     let number=0
    let goodsId = this.data.productList[0].goods_id
    // let number = this.data.productList[0].goods_number
    console.log("goodsId",goodsId);
    
      
    let productId = this.data.productList[0].id
    console.log("productId",productId);
    
    console.log(goodsId,number,productId);
     let token = wx.getStorageSync('token');
      console.log(token);
      if (goodsId,number,productId) {
        number=number+1      //添加商品的个数
        console.log("number",number);
        
        myRequest({
          url:api.addCart,
          header:{'X-Nideshop-Token': token},
          method:"POST", 
          data:{
            goodsId,number,productId
          },
          success:(res)=>{
            console.log("需要加入购物车的商品",res)
            wx.showToast({
              title: "已加入购物车", //提示的内容,
              icon: 'success', //图标,
              duration: 2000, //延迟时间,
               success:(res)=>{
                wx.switchTab({ url:'../../../pages/cart/cart' });
               }
              
            });
         
          }

        })
      }
  },

  // 点击返回首页
  toIndex(){
    wx.reLaunch({
      url: '../../../pages/index/index',
    })
  },
  
  // 点击 大家都在看的商品列表
  onGoodsList(e){
    // 页面回到顶部
    wx.pageScrollTo({
      scrollTop: 0
    })

    // 重置轮播图片的位置
    this.setData({
      current: 1
    })

    console.log('大家都在看',e);
    let goodsRelatedId = e.currentTarget.dataset.goodslistid;
    // 请求商品详情接口获取重新渲染页面的数据
    myRequest({
      url: api.goodsDetail,
      data: { 
          id: goodsRelatedId
      },
      success: (res)=>{
        console.log('goodsRelatedId数据',res);
        let data = res.data.data;
        this.setData({
          info: data.info,
          issue: data.issue,
          attribute: data.attribute,
          brand: data.brand.id ? data.brand : '',
          comment: data.comment,
          gallery: data.gallery
        });

        // wxParse文本解析
        var goods_desc = data.info.goods_desc;
        WxParse.wxParse('goods_desc', 'html', goods_desc, this, 0);
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // 设置导航栏标题
    wx.setNavigationBarTitle({
      title: '商品详情'
    })

    let newGoodsId = Number(options.NewGoods_id);
    let hotGoodsId = Number(options.HotGoods_id);
    let goodsListId = Number(options.GoodsList_id);
    let goodsId = Number(options.goodsId);
    console.log("options",options);

    // 判断点击获取对应商品详情接口的id是否为存在
    if(newGoodsId || hotGoodsId || goodsListId || goodsId){
      // 请求商品详情接口获取渲染页面的数据
      myRequest({
        url: api.goodsDetail,
        data: { 
            id: newGoodsId || hotGoodsId || goodsListId || goodsId
        },
        success: (res)=>{
          console.log('goodsDetail数据',res);
          let data = res.data.data;
          this.setData({
            info: data.info,
            issue: data.issue,
            attribute: data.attribute,
            brand: data.brand.id ? data.brand : '',
            comment: data.comment,
            gallery: data.gallery,
            productList:data.productList  //需要加入购物车的商品
          });

          // wxParse文本解析
          var goods_desc = data.info.goods_desc;
          WxParse.wxParse('goods_desc', 'html', goods_desc, this, 0);
        }
      });

      // 请求商品详情页的关联商品接口获取渲染页面的数据
      myRequest({
        url: api.goodsRelated,
        data: { 
            id: newGoodsId || hotGoodsId || goodsListId || goodsId
        },
        success: (res)=>{
          console.log('goodsRelated数据',res);
          let data = res.data.data;
          this.setData({
            goodsList: data.goodsList
          });
        }
      });
    }

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showToast({
      title: '没有更多内容啦~',
      icon: 'none',
      duration: 1000
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

})